package org.ppassantini.cham.core.business.security.dao.impl;

import java.util.List;

import org.ppassantini.cham.core.business.security.dao.UsersDao;
import org.ppassantini.cham.core.business.security.entity.Users;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author ppassantini
 * 
 */
public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.security.dao.UsersDao#save(org.ppassantini
	 * .cham.core.business.security.entity.Users)
	 */
	@Override
	public void save(Users user) {
		getHibernateTemplate().save(user);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ppassantini.cham.core.business.security.dao.UsersDao#update(org.
	 * ppassantini.cham.core.business.security.entity.Users)
	 */
	@Override
	public void update(Users user) {
		getHibernateTemplate().update(user);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.security.dao.GenericDao#findByKey(
	 * java.lang.Object)
	 */
	@Override
	public Users findByKey(Integer idusers) {
		List<Users> list = getHibernateTemplate().find(
				"from Users u where u.idusers=?", idusers);

		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ppassantini.cham.core.business.security.dao.UsersDao#delete(org.
	 * ppassantini.cham.core.business.security.entity.Users)
	 */
	@Override
	public void delete(Users user) {
		getHibernateTemplate().delete(user);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.security.dao.UsersDao#verifyUser(java
	 * .lang.String, java.lang.String)
	 */
	@Override
	public Users verifyUser(String userName, String password) {
		String[] param = { userName, password };
		List<Users> list = getHibernateTemplate().find(
				"from Users u where u.username=? and u.pwd=?", param);

		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

}
